package com.gordon.moiveplayer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import static android.content.ContentValues.TAG;


public class PlayActivity extends Activity implements SurfaceHolder.Callback{
    final public static String TAG = "PlayActivity";
    public SurfaceView surfaceView = null;
    public MediaPlayer mediaPlayer = null;
    public SurfaceHolder holder = null;
    public Display display = null;
    public String playPath = null;
    //public VideoView videoView = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        //videoView = (VideoView) findViewById(R.id.video_view);
        surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String path = bundle.getString("name");
        Log.d(TAG,"Playe path :"+path);
        playPath = path;
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(path);
        }catch (Exception e) {
            e.printStackTrace();
        }
        holder = surfaceView.getHolder();

        holder.addCallback(this);

        display =  this.getWindowManager().getDefaultDisplay();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG,"onPrepared");
                int fW = mediaPlayer.getVideoWidth();
                int fH = mediaPlayer.getVideoHeight();
                Point point = new Point();
                display.getSize(point);
                Log.d(TAG,"fW="+fW+",fH="+fH+",x="+point.x+",y="+point.y);
                if (fW > point.x || fH > point.y){
                    Log.d(TAG,"x="+point.x+"y="+point.y);
                }
               // fW = 1280;
               // fH = 720;
                //LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(fW, fH);
               // lp.gravity = Gravity.CENTER;
              //  surfaceView.setLayoutParams(lp);
                mediaPlayer.start();
            }
        });
        mediaPlayer.prepareAsync();


//        videoView.setVideoPath(path);
//        videoView.setMediaController(new MediaController(this));
//        videoView.start();
//        videoView.requestFocus();
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG,"surface created");
        mediaPlayer.setDisplay(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG,"surface changed width="+width+",height="+height);
       // holder.setFixedSize(720,576);
       // holder.setSizeFromLayout();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG,"surface destroryed");
    }

    class PlaySource {
        public int curTime;
        public int totleTime;
        String playFileName;
    }
    public Runnable PlayStateRunnable = new Runnable() {
        @Override
        public void run() {
            if (mediaPlayer != null){
                PlaySource playSource = new PlaySource();
                playSource.curTime = mediaPlayer.getCurrentPosition();
                playSource.totleTime = mediaPlayer.getDuration();
                playSource.playFileName = playPath;
                //mediaPlayer.
            }
        }
    };

    public static final int SHOW_STATE = 1;
    public static final int HIDE_STATE = 2;
    Handler stateHandle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
           // super.handleMessage(msg);
            switch (msg.what){
                case SHOW_STATE:
                    int curTime = mediaPlayer.getCurrentPosition();
                    sendEmptyMessageDelayed(SHOW_STATE,1000);
                    break;
                case HIDE_STATE:
                    removeMessages(SHOW_STATE);
                    break;
            }
        }
    };


}
